#ifndef GLHANDLE_H
#define GLHANDLE_H
class GLHandler : public Handler 
{
public:
	GLHandler(GLView * glView, Model * model):glView(glView),is_create(false), model (model){}
	~GLHandler(){}

public:
	int lButtonDown(WPARAM state, int x, int y)
	{
		//::MessageBox(0, L"Draw.", L"Error", MB_ICONEXCLAMATION | MB_OK);
		model->setMousePos(model->cullingView, x, y);
		if(state == MK_LBUTTON)
		{
			model->setMouseLeft(model->cullingView, true);
		}
		return 0;
	}
	int lButtonUp(WPARAM state, int x, int y)
	{
		
		model->setMousePos(model->cullingView, x, y);
		model->setMouseLeft(model->cullingView, false);
		if (model->cullingView.viewMode == EDIT)
		{
			model->paint();
			model->setUpdated(model->cullingView, true);
			model->setUpdated(model->mappingView, true);
		}
		
		return 0;
	}
	int rButtonDown(WPARAM state, int x, int y)
	{
		model->setMousePos(model->cullingView, x, y);
		if(state == MK_RBUTTON)
		{
			model->setMouseRight(model->cullingView, true);
		}
		return 0;		
	}
	int rButtonUp(WPARAM state, int x, int y)
	{
		model->setMousePos(model->cullingView, x, y);
		model->setMouseRight(model->cullingView, false);
		if (model->cullingView.viewMode == EDIT)
		{
			model->pick_face();
			model->setUpdated(model->cullingView, true);
			model->setUpdated(model->mappingView, true);
			//model->setUpdated(true);
		}
		return 0;
	}
	int mouseMove(WPARAM state, int x, int y)
	{
		//model->moveBrush(model->cullingView, x, y);
		//model->setUpdated(model->cullingView, true);

		if (model->cullingView.viewMode == VIEW)
		{
			if(state == MK_LBUTTON)
			{
				if (model->getMouseLeft(model->cullingView) )
				{	
					model->rotateCamera(model->cullingView, x, y);
					EnterCriticalSection(&CriticalSection); 
					model->setUpdated(model->cullingView, true);
					model->setUpdated(model->mappingView, true);
					model->cullingState = true ;
					LeaveCriticalSection(&CriticalSection); 
				}
			}
			if(state == MK_RBUTTON)
			{
				if (model->getMouseRight(model->cullingView))
				{
					model->zoomCamera(model->cullingView, y);
					//model->zoomCamera(model->mappingView, y);
					model->mappingView.cameraDis = model->cullingView.cameraDis ;
					EnterCriticalSection(&CriticalSection); 
					model->setUpdated(model->cullingView, true);
					model->setUpdated(model->mappingView, true);
					model->cullingState = true ;
					LeaveCriticalSection(&CriticalSection); 
				}
			}
		}
		else if (model->cullingView.viewMode == EDIT)
		{
			
			model->movePerpBrush(model->cullingView, x, y);
			EnterCriticalSection(&CriticalSection); 
			model->setUpdated(model->cullingView, true);
			model->setUpdated(model->mappingView, true);
			model->cullingState = true ;
			LeaveCriticalSection(&CriticalSection); 
			
		}
		/*
		if(state == MK_LBUTTON)
		{
			if (model->getMouseLeft(model->cullingView) )
			{
				model->rotateCamera(model->cullingView, x, y);
				EnterCriticalSection(&CriticalSection); 
				model->setUpdated(model->cullingView, true);
				model->setUpdated(model->mappingView, true);
				model->cullingState = true ;
				LeaveCriticalSection(&CriticalSection); 
			}
		}
		if(state == MK_RBUTTON)
		{
			if (model->getMouseRight(model->cullingView))
			{
				model->zoomCamera(model->cullingView, y);
				//model->zoomCamera(model->mappingView, y);
				EnterCriticalSection(&CriticalSection); 
				model->setUpdated(model->cullingView, true);
				model->setUpdated(model->mappingView, true);
				model->cullingState = true ;
				LeaveCriticalSection(&CriticalSection); 
			}
		}
		*/
		return 0;
	}

	int create() 
	{
		//model->loadModel("Z:\\workspace\\data\\cube3.obj"); 
		//SetTimer(this->glView->getHWND(), 
		/*Fixe me here settimer*/
		
		return 0;
	}
	int size(int w, int h, WPARAM type)
	{
		model->setUpdated(model->cullingView, true);
		model->setWindowSize(model->cullingView, w, h);
		return 0; 
	}
	int timer(WPARAM id, LPARAM lParam)
	{
		glView->Draw();
		return 0;
	}
	int paint()
	{
		if (!is_create)
		{
			glView->EnableOpenGL();
			//model->init();
			model->setUpdated(model->cullingView, true);
			is_create = true;
		}

		model->setUpdated(model->cullingView, true);
		//model->draw();
		//glView->Draw();
		return 0;
	}
	int destroy()
	{
		// close OpenGL Rendering context
		
	    glView->DisableOpenGL();
		::DestroyWindow(handle);
		return 0;
	}

protected:
	GLView * glView ;
	Model * model ;
	bool is_create ;
};

#endif